#ifndef H264_DECODE_GST_H
#define H264_DECODE_GST_H

#include <ImageType.hpp>
#include <gst/gst.h>
#include <gst/app/app.h>
#include <thread>
#include <mutex>
#include <atomic>
#include <queue>
#include <functional>
#include <condition_variable>

//在调用下面的类时，要提前完成全局初始化
void GlobalInit();
//用于设置pipeline的名称
static int ID_counter=0;

class h264decode_gst
{
  public:
    h264decode_gst();  //Init
    virtual ~h264decode_gst();
    void InputNewImage(unsigned char* input_buf,std::size_t bufsize,long frameID);
    ImageType GetNewImage();
    std::function<void(ImageType& image)> funcHandleData;
  private:
    //声明友元，这样C函数能够访问C++私有成员
    friend void on_enough_appsrc(GstElement* appsrc,gpointer udata);
    friend void on_needdata_appsrc(GstElement* appsrc,guint length,gpointer udata);
    friend gboolean bus_call(GstBus *bus,GstMessage *msg,gpointer data);

    GMainLoop* loop;
    GstElement* pipeline;
    GstElement* appsrc;
    GstElement* queue;
    GstElement* h264parse;
    GstElement* openh264dec;
    GstElement* videoConvert;
    GstElement* cudadownload;
    GstElement* appsink;

    GstBus* bus;
    guint busWatch;
    GstMessage* msg;

    std::thread thread_receiveImage;
    std::thread thread_mainRun;
    void ThreadWork_receiveImage();
    void ThreadWork_mainRun();

    std::mutex mutFrameQueue;
    std::queue<long> frameQueue;

    std::mutex mutResData;
    std::atomic_bool resReady=false;
    ImageType imageData;
    std::condition_variable condRes;

    std::atomic_bool pushDataFlag=true;
};

#endif
